<?php
declare (strict_types = 1);

namespace app\command;

use app\model\zs\ZsBusiness;
use app\model\zs\ZsMemberCoupon;
use app\model\zs\ZsMemberCouponProvide;
use app\repository\BusinessRepository;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Db;

class BusinessLoginid extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('business:loginid')
            ->setDescription('批量填充business_legal、business_meal表的bid字段');
    }

    protected function execute(Input $input, Output $output)
    {
        $today_str = date('Y-m-d H:i:s');
        $table_legal = 'business_legal';
        $table_meal = 'business_meal';

        // 查询所有正常状态的商户
        // $count = Db::connect('zs')->table('business')->where('status', 1)->where('loginid', '<>', '')->count();
        $loginid_rel = Db::connect('zs')->table('business')->where('status', 1)->where('loginid', '<>', '')->column('id', 'loginid');
        $list_legal = Db::connect('zs')->table($table_legal)->where('loginid', '<>', '')->select();
        $list_meal = Db::connect('zs')->table($table_meal)->where('loginid', '<>', '')->select();

        foreach ($list_legal as $item_legal) {
            $cur_id = $item_legal['id'];
            $bid = $loginid_rel[$item_legal['loginid']] ?? 0;
            if (empty($bid) || $item_legal['bid'] == $bid) {
                continue;
            }
            Db::connect('zs')->table($table_legal)->where('id', $cur_id)->update(['bid' => $bid, 'update_date' => $today_str]);
        }

        foreach ($list_meal as $item_meal) {
            $cur_id = $item_meal['id'];
            $bid = $loginid_rel[$item_meal['loginid']] ?? 0;
            if (empty($bid) || $item_meal['bid'] == $bid) {
                continue;
            }
            Db::connect('zs')->table($table_meal)->where('id', $cur_id)->update(['bid' => $bid, 'update_date' => $today_str]);
        }

        // 指令输出
        $output->writeln("ok");
        return;
    }
}
